<template>
	<view>
		<uni-list>
			<uni-list-item v-for="item in list" :title="item.content" :rightText="item.time" />
		</uni-list>
		<uni-load-more iconType="circle" :status="status" @clickLoadMore="getMore" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			apiUrl: 'http://localhost:8080/',
			activityId: 1,
			uid: 1,
			type: '',
			
			status: 'more',
			list: [],
			pageNum: 1,
			pageSize: 5
		};
	},
	onLoad: function(option) {
		this.activityId = option.activityId;
		this.uid = option.uid;
		this.type = option.type;
		
		this.pageNum = 1;
		this.getList();
		this.setRead();
	},
	onPullDownRefresh() {
		this.pageNum = 1;
		this.getList();
	},
	methods: {
		getMore() {
			this.pageNum++;
			this.getList();
		},
		getList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/single',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: this.type,
					toUid: this.uid,
					activityId: this.activityId,
					pageNum: this.pageNum,
					pageSize: this.pageSize
				},
				success: res => {
					console.log(res.data);
					if (this.pageNum <= 1) {
						this.list = [];
					}
					let originSize = this.list.length;
					this.list.push.apply(this.list, res.data.records);
					let deltaSize = res.data.records.length;

		
					if (res.data.total <= originSize + deltaSize) {
						this.status = 'noMore';
					} else {
						this.status = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		setRead() {
			uni.request({
				method: 'PUT',
				url: this.apiUrl + 'message',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: this.type,
					toUid: this.uid,
					activityId: this.activityId
				},
				success: res => {
					console.log(res.data);
					
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		}
	}
};
</script>

<style></style>
